﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParamSetCollection.Devices.CodeScanner
{
    /// <summary>
    /// 原始码事件
    /// </summary>
    /// <param name="position">扫码枪位置</param>
    /// <param name="number">扫码枪编号</param>
    /// <param name="code">原始条码</param>
    public delegate void OnRawCodeEventHandler(string position, string code);

    /// <summary>
    /// 经过筛选逻辑的有效码
    /// </summary>
    /// <param name="position">扫码枪位置</param>
    /// <param name="number">扫码枪编号</param>
    /// <param name="code">原始条码</param>
    public delegate void OnValidCodeEventHandler(string position, string code);

    public delegate void OnErrorMsgEventHandler(string position, string msg);

    public class ScannerHelper
    {

        #region============================== 变量 ====================================



        public event OnRawCodeEventHandler OnRawCodeEvent;

        public event OnValidCodeEventHandler OnValidCodeEvent;

        public event OnErrorMsgEventHandler OnErrorMsgEvent;


        Dictionary<string, Scanner> scannerDic = new Dictionary<string, Scanner>();
        #endregion=====================================================================


        #region============================== 属性 ====================================


        #endregion=====================================================================


        #region============================= 内部函数 ================================
        #endregion====================================================================


        #region============================= 保护函数 ================================
        #endregion====================================================================


        #region============================= 公开函数 ================================
        public ScannerHelper()
        {

        }

        /// <summary>
        /// 加入串口扫码枪
        /// </summary>
        /// <param name="position">扫码枪位置描述，区分不同位置的枪</param>
        /// <param name="comPort">串口编号，值必须>=1</param>
        /// <param name="endMark">扫码结束符</param>
        /// <param name="sameCodeFilter">是否启用重复扫码过滤器</param>
        public void AddInSerialScanner(string position, int comPort, string endMark = "\r\n", bool sameCodeFilter = true)
        {
            if (scannerDic.ContainsKey(position) == false)
            {
                Scanner scanner = new SerialScanner(position, comPort,endMark,sameCodeFilter);
                scannerDic.Add(position, scanner);
                scanner.OnRawCodeEvent += Scanner_OnRawCodeEvent;
                scanner.OnValidCodeEvent += Scanner_OnValidCodeEvent;
                scanner.OnErrorMsgEvent += Scanner_OnErrorMsgEvent;
            }



        }



        public void AddInEthernetScanner(string position, string ip, int port, string endMark = "\r\n", bool sameCodeFilter = true)
        {
            if (scannerDic.ContainsKey(position) == false)
            {

            }



        }

        //设置过滤器,正则表达式
        public void SetFilter(string position, string formula)
        {
            if (scannerDic.ContainsKey(position) == true)
            {
                scannerDic[position].FilterCode = formula;

            }
        }


        public void Start()
        {
            List<string> keys = scannerDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                scannerDic[keys[i]].Start();
            }
        }

        public void Stop()
        {
            List<string> keys = scannerDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                scannerDic[keys[i]].Stop();
            }
        }

        public void SoftTriggerStart(string position, string cmd)
        {
            scannerDic[position].SoftTriggerStart(cmd);
        }

        public void SoftTriggerStop(string position, string cmd)
        {
            scannerDic[position].SoftTriggerStop(cmd);
        }
        #endregion====================================================================


        #region============================= 消息响应 ================================
        private void Scanner_OnErrorMsgEvent(string position, string msg)
        {
            OnErrorMsgEvent?.Invoke(position, msg);
        }

        private void Scanner_OnValidCodeEvent(string position, string code)
        {
            OnValidCodeEvent?.Invoke(position, code);
        }

        private void Scanner_OnRawCodeEvent(string position, string code)
        {
            OnRawCodeEvent?.Invoke(position, code);
        }

        #endregion====================================================================


    }
}
